package movie.dao;

import java.util.*;

import movie.modle.Movie;

/**
 * 实现增删改查
 * @author 王荣
 *
 */
public class MovieDaoImpl implements MovieDao{
	//创建一个集合充当影片数据库
	private ArrayList<Movie> movies = new ArrayList<>();
	
	{
		addMovie(new Movie(0,"我不是药神", "文牧野", "徐峥", "剧情", new Date(),80,10));
		addMovie(new Movie(0,"西虹市首富", "闫非", "沈腾", "喜剧", new Date(),90,9));
		addMovie(new Movie(0,"羞羞的铁拳", "宋阳", "艾伦", "喜剧", new Date(),50,7));
		addMovie(new Movie(0,"少年的你", "曾国祥", "周冬雨", "爱情", new Date(),70,8));
		addMovie(new Movie(0,"流浪地球", "郭帆", "吴京", "科幻", new Date(),60,6));
	}
	
	
	/**
	 * 获取集合
	 * @return
	 */
	public ArrayList<Movie> getMovies() {
		return movies;
	}


	private int total = 0;//电影总数
	/**
	 * 增加影片
	 * @param movie 影片对象
	 * @return 增加成功返回true,失败返回false
	 */
	public boolean addMovie(Movie movie){
		movie.setId(++total);
		return movies.add(movie);
	}
	
	
	/**
	 * 删除影片
	 * @param movie 影片对象
	 * @return 删除成功返回true,失败返回false
	 */
	public boolean delMovie(Movie movie){
		return movies.remove(movie);
	}
	
	
	/**
	 * 修改影片
	 * @param id 影片ID
	 * @param movie 新的信息
	 * @return 修改成功返回true,失败返回false
	 */
	public boolean modifyMovie(int id,Movie movie){
		Movie set = movies.set(--id, movie);
		//set不为空表示修改成功，否则修改失败
		return (set != null) ;
	}
	
	
	/**
	 * 查找影片
	 * @param id 影片ID
	 * @return 如果ID存在返回影片，不存在返回null
	 */
	public Movie searchMovie(int id){
		if(id > movies.size() || id <= 0)	return null;
		return movies.get(--id);
	}
	
	
}
